/**
 * 
 */
package com.qdairlines.common;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * @author pangyannan
 * @date 2017年1月17日
 * @company QingDao Airlines
 * @description 
 */
public class WxLinkUtil {
	//各系统的微信登录地址，各系统应更改为自己的地址
	private static String myLoginByWx = "http://baseweb.qdairlines.com/login/loginByWx.do";	
	
	//微信集成登录wxlink服务器请求地址
	private static String wechatLink =  "http://wxlink.qdairlines.com/wxLogin.do";
	private static String getWorkcodeUrl = "http://wxlink.qdairlines.com/getWorkcode.do";
	private static String getWxUserInfoUrl = "http://wxlink.qdairlines.com/getUser.do";
	
	/*
	//本地集成测试地址
	private static String myLoginByWx = "http://79421051.tunnel.2bdata.com/WeChatLink/loginByWx.do";	
	//微信集成登录wxlink服务器请求地址
	private static String wechatLink =  "http://79421051.tunnel.2bdata.com/WeChatLink/wxLogin.do";
	private static String getWorkcodeUrl = "http://79421051.tunnel.2bdata.com/WeChatLink/getWorkcode.do";
	private static String getWxUserInfoUrl = "http://79421051.tunnel.2bdata.com/WeChatLink/getUser.do";
	*/

	/**
	 * 由于微信消息免登陆url非常长，各系统在向MQ发送微信推送消息时，用该方法生成免登陆地址
	 * @param state
	 * @return
	 */

	public static String redirectUrlEncode(String redirectUrl,Map<String,String> map,String workcode){
		//将转发参数编码,参数用$连接
		StringBuffer mapStr =  new StringBuffer();
		mapStr.append("");
		for (String key : map.keySet()) {
			String str = map.get(key); 
			mapStr.append(key+"="+str+"$");
		}
		
		//将workcode加密，用于防止消息转发登录
		mapStr.append("wxworkcode="+WorkcodeEncode(workcode));
		
		return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
							+"wxcfabdcf087a0852a"
							+"&redirect_uri="
							+wechatLink
							+"&response_type=code&scope=SCOPE&state="
							+myLoginByWx
							+"?redirectUrl="
							+redirectUrl
							+"?"
							+mapStr
							+"#wechat_redirect";
	
	
	}
	
	/**
	 * 转发参数解码，将$替换为url的连接字符&
	 * @param redirectUrl
	 * @return
	 */
	public static String redirectUrlDecode(String redirectUrl){
		return redirectUrl.replace("$","&");
	}
	
	/**
	 * 向微信集成登录后台获取workcode
	 * @param key
	 * @return
	 */
	public static String getWorkcode(String key){
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("key", key);
		String workcode = 	HttpUtil.sendGet(getWorkcodeUrl, parameters);
		return workcode;
	}
	
	public static String WorkcodeEncode(String workcode){	
		return MD5Encoder.encode(workcode);
	}
	/**
	 * 验证redirectUrl中的workcode是否是合法的
	 * @param redirectUrl
	 */
	public static boolean validateWorkcode(String redirectUrl,String workcode){
		Boolean flag = false;
		if(redirectUrl.indexOf("$wxworkcode") != -1){
			//根据redirectUrl解析出来加密的wxworkcode，不过，相应的加密，也应当用 WxLinkUtil.redirectUrlEncode
			String wxworkcode =redirectUrl.substring(redirectUrl.indexOf("$wxworkcode")).split("=")[1];
			if(StringUtils.isEmpty(wxworkcode) || StringUtils.isEmpty(workcode)){
				flag = false;
			}else{
				flag = wxworkcode.equals(WorkcodeEncode(workcode));
			}
		}
		return flag;
	}

	/**
	 * 向微信集成登录后台获取user信息
	 * @param key
	 * @return
	 */
	public static String getUser(String key){
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("key", key);
		String workcode = 	HttpUtil.sendGet(getWxUserInfoUrl, parameters);
		return workcode;
	}
}
